﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Bussiness_Objects;
using DataObjects.ADO;

namespace WindowsFormsApplication1
{
    public partial class ThongTinDocGia : DevComponents.DotNetBar.Controls.SlidePanel
    {
        DataTable _tableDocGia;
        SqlDocGia _sqlDocGia = new SqlDocGia();
        DocGia _docgia;

        DataTable _tableLoaiDG;
        SqlLoaiDocGia _sqlLoaiDG = new SqlLoaiDocGia();

        public ThongTinDocGia()
        {
            InitializeComponent();
        }

        private void loadTableDocGia()
        {
            _tableDocGia = _sqlDocGia.GetTable();
            dgvDocGia.DataSource = _tableDocGia;
        }

        private void createTableDocGia()
        {
            loadTableDocGia();

            dgvDocGia.Columns["MaDocGia"].HeaderText = "Mã Độc Giả";
            dgvDocGia.Columns["MaDocGia"].ReadOnly = true;

            dgvDocGia.Columns["MaLoaiDocGia"].HeaderText = "Mã Loại Độc Giả";
            dgvDocGia.Columns["MaLoaiDocGia"].Visible = false;

            dgvDocGia.Columns["TenLoaiDocGia"].HeaderText = "Tên loại độc giả";
            dgvDocGia.Columns["HoTen"].HeaderText = "Họ Tên";
            dgvDocGia.Columns["NgaySinh"].HeaderText = "Ngày Sinh";
            dgvDocGia.Columns["DiaChi"].HeaderText = "Địa chỉ";
            dgvDocGia.Columns["NgayLapThe"].HeaderText = "Ngày làm thẻ";
            dgvDocGia.Columns["NgayHetHan"].HeaderText = "Ngày hết hạn";
            dgvDocGia.Columns["NgayHetHan"].ReadOnly = true;
            dgvDocGia.Columns["TongNo"].HeaderText = "Tổng nợ";
        }

        private void loadTableLoaiDG()
        {
            _tableLoaiDG = _sqlLoaiDG.GetTable();
            dgvLoaiDG.DataSource = _tableLoaiDG;
        }

        private void createTableLoaiDG()
        {
            loadTableLoaiDG();

            dgvLoaiDG.Columns["MaLoaiDocGia"].HeaderText = "Mã Loại Độc Giả";
            dgvLoaiDG.Columns["MaLoaiDocGia"].ReadOnly = true;
            dgvLoaiDG.Columns["MaLoaiDocGia"].AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader;
            dgvLoaiDG.Columns["TenLoaiDocGia"].HeaderText = "Tên Loại Độc Giả";
        }

        public void UpdateSize()
        {
            this.Size = new Size(((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size.Width - 17,
                                    ((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size.Height - 38);
        }

        public void ShowSlide()
        {
            this.IsOpen = true;
            createTableDocGia();
            createTableLoaiDG();

            this.dgvLoaiDG.CellValueChanged += new DataGridViewCellEventHandler(dgvLoaiDG_CellValueChanged);
            UpdateSize();
        }

        public void UpdateThoiHanThe(string ngayGiaHan)
        {
            int index = dgvDocGia.CurrentRow.Index;
            _docgia = setDataDocGia(index);
            _docgia.NgayLapThe = ngayGiaHan;
            MessageBox.Show(_sqlDocGia.Update(_docgia));
            loadTableDocGia();
        }

        public void UpdateDocGia(DocGia docGia)
        {
            string result = _sqlDocGia.Update(docGia);
            if (result == "Success")
            {
                MessageBox.Show("Đã cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                loadTableDocGia();
            }
            else
                MessageBox.Show(result, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void DeteleDocGia()
        {
            if (DialogResult.Yes == MessageBox.Show("Bạn có chắc muốn xóa độc giả \"" + dgvDocGia["HoTen", dgvDocGia.CurrentRow.Index].Value.ToString() + "\"",
                                                    "Cảnh báo",
                                                    MessageBoxButtons.YesNo,
                                                    MessageBoxIcon.Warning))
            {
                _docgia = new DocGia();
                DataRow row = _tableDocGia.Rows[dgvDocGia.CurrentRow.Index];
                _docgia.MaDocGia = (int)row[0];
                string result = _sqlDocGia.Delete(_docgia);
                if (result != "Success")
                    MessageBox.Show("Lỗi : " + result, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    createTableDocGia();
            }
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            DeteleDocGia();
        }

        private void butSearch_Click(object sender, EventArgs e)
        {

        }

        private void menu_Thoat_Click(object sender, EventArgs e)
        {
            this.IsOpen = false;
            this.dgvLoaiDG.CellValueChanged -= new DataGridViewCellEventHandler(dgvLoaiDG_CellValueChanged);
        }

        private void butRefresh_Click(object sender, EventArgs e)
        {
            loadTableDocGia();
            loadTableLoaiDG();
        }

        private void dgvDocGia_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            e.Cancel = true;
            DeteleDocGia();
        }

        private void butThem_Click(object sender, EventArgs e)
        {
            MainForm._lapTheDG.BringToFront();
            MainForm._lapTheDG.ShowSlide();
        }

        private void butEdit_Click(object sender, EventArgs e)
        {
            _docgia = setDataDocGia(dgvDocGia.CurrentRow.Index);
            EditDocGia edit = new EditDocGia(_docgia);
            edit.Show();
        }

        private DocGia setDataDocGia(int index)
        {
            DocGia temp = new DocGia();
            temp.MaDocGia = int.Parse(_tableDocGia.Rows[index]["MaDocGia"].ToString());
            temp.HoTen = _tableDocGia.Rows[index]["HoTen"].ToString();
            temp.MaLoaiDocGia = int.Parse(_tableDocGia.Rows[index]["MaLoaiDocGia"].ToString());
            temp.DiaChi = _tableDocGia.Rows[index]["DiaChi"].ToString();
            temp.Email = _tableDocGia.Rows[index]["Email"].ToString();
            temp.NgatHetHan = _tableDocGia.Rows[index]["NgayHetHan"].ToString();
            temp.NgayLapThe = _tableDocGia.Rows[index]["NgayLapThe"].ToString();
            temp.NgaySinh = _tableDocGia.Rows[index]["NgaySinh"].ToString();
            temp.TongNo = (long)float.Parse(_tableDocGia.Rows[index]["TongNo"].ToString());
            return temp;
        }

        private void dgvLoaiDG_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            dgvLoaiDG.CellValueChanged -= new DataGridViewCellEventHandler(dgvLoaiDG_CellValueChanged);

            if (!string.IsNullOrEmpty(dgvLoaiDG[e.ColumnIndex, e.RowIndex].Value.ToString()))
            {

                dgvLoaiDG[e.ColumnIndex, e.RowIndex].Value = Bussiness_Objects.BusinessRule.ValiadateText.TrimText(dgvLoaiDG[e.ColumnIndex, e.RowIndex].Value.ToString());
                /************************************************************************/
                /* Insert Loai Doc Gia                                                  */
                /************************************************************************/
                if (dgvLoaiDG[0, e.RowIndex].Value.ToString() == "")
                {
                    LoaiDocGia temp = new LoaiDocGia();
                    temp.TenLoaiDocGia = dgvLoaiDG[e.ColumnIndex, e.RowIndex].Value.ToString();
                    string result = _sqlLoaiDG.Insert(temp);
                    if (result != "Success")
                    {
                        MessageBox.Show(result, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                /************************************************************************/
                /* Edit Loai Doc Gia                                                    */
                /************************************************************************/
                {
                    if (MessageBox.Show("Thay đổi tên loại độc giả ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
                    {
                        LoaiDocGia temp = new LoaiDocGia(
                            int.Parse(dgvLoaiDG[e.ColumnIndex - 1, e.RowIndex].Value.ToString()),
                            dgvLoaiDG[e.ColumnIndex, e.RowIndex].Value.ToString());
                        string result = _sqlLoaiDG.Update(temp);
                        if (result != "Success")
                        {
                            MessageBox.Show(result, "Lổi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            loadTableLoaiDG();
                        }
                    }
                }
            }
            loadTableLoaiDG();
            dgvLoaiDG.CellValueChanged += new DataGridViewCellEventHandler(dgvLoaiDG_CellValueChanged);
        }

        private void DeleteLoaiDG()
        {
            if (MessageBox.Show("Muốn xóa Loại độc giả \"" + dgvLoaiDG[1, dgvLoaiDG.CurrentRow.Index].Value.ToString() + "\"",
                                "Cảnh báo",
                                MessageBoxButtons.YesNo,
                                MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                LoaiDocGia loaiDG = new LoaiDocGia();
                loaiDG.MaLoaiDocGia = int.Parse(dgvLoaiDG[0, dgvLoaiDG.CurrentRow.Index].Value.ToString());
                string result = _sqlLoaiDG.Delete(loaiDG);
                if (result != "Success")
                    MessageBox.Show(result, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                    loadTableLoaiDG();
            }
        }

        private void butDeleteLoaiDG_Click(object sender, EventArgs e)
        {
            DeleteLoaiDG();
        }

        private void dgvLoaiDG_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            e.Cancel = true;
            DeleteLoaiDG();
        }

        private void butEditLoaiDG_Click(object sender, EventArgs e)
        {
            dgvLoaiDG.BeginEdit(true);
        }

        private void butThemLoaiDG_Click(object sender, EventArgs e)
        {
            dgvLoaiDG.CurrentCell = dgvLoaiDG.Rows[dgvLoaiDG.Rows.Count - 1].Cells[1];
            dgvLoaiDG.BeginEdit(true);
        }

        private void butThuTien_Click(object sender, EventArgs e)
        {
            MainForm._thuTienPhat.BringToFront();
            MainForm._thuTienPhat.ShowSlide();
            MainForm._thuTienPhat.SetValue(int.Parse(dgvDocGia[0, dgvDocGia.CurrentRow.Index].Value.ToString()));
        }

        private void butMuonSach_Click(object sender, EventArgs e)
        {
            MainForm._muonSach.BringToFront();
            MainForm._muonSach.ShowSlide();
            MainForm._muonSach.SetValue(int.Parse(dgvDocGia[0, dgvDocGia.CurrentRow.Index].Value.ToString()));
        }

        private void butTraSach_Click(object sender, EventArgs e)
        {
            MainForm._traSach.BringToFront();
            MainForm._traSach.ShowSlide();
            MainForm._traSach.SetValue(int.Parse(dgvDocGia[0, dgvDocGia.CurrentRow.Index].Value.ToString()));
        }

        private void butGiaHan_Click(object sender, EventArgs e)
        {
            int index = dgvDocGia.CurrentRow.Index;
            GiaHanThe giaHan = new GiaHanThe(int.Parse(dgvDocGia[0, index].Value.ToString()), dgvDocGia[1, index].Value.ToString());
            giaHan.Show();
        }

        private void checkKeyPress_IntValue(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsPunctuation(e.KeyChar))
                e.Handled = true;
        }

        private void searchGribView(string column, string value)
        {
            for (int i = 0; i < dgvDocGia.Rows.Count; i++)
            {
                if (dgvDocGia[column, i].Value.ToString() == value)
                {
                    dgvDocGia[column, i].Selected = true;
                    break;
                }
            }
        }

        private void txtMaDG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!String.IsNullOrEmpty(txtMaDG.Text))
                {
                    searchGribView("MaDocGia", txtMaDG.Text);
                }
            }
        }

        private void txtTenDG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtTenDG.Text = Bussiness_Objects.BusinessRule.ValiadateText.UpcaseName(txtTenDG.Text);
                if (!String.IsNullOrEmpty(txtTenDG.Text))
                {
                    searchGribView("HoTen", txtTenDG.Text);
                }
            }
        }
    }
}